package top.dotgo.web.admin.oauth.kaptcha;

import cn.hutool.core.util.RandomUtil;
import com.google.code.kaptcha.text.impl.DefaultTextCreator;

/**
 * @author jornl
 * @date 2020/3/4 0:49 星期三
 */
public class KaptchaTextCreator extends DefaultTextCreator {
    public static final int MUL = 2;
    public static final int DIV = 3;
    private static final int ADD = 0;
    private static final int SUB = 1;

    @Override
    public String getText() {
        int type = RandomUtil.randomInt(0, 4);
        StringBuilder result = new StringBuilder();
        int a = RandomUtil.randomInt(1, 10);
        int b = RandomUtil.randomInt(1, 10);

        if (type == ADD) {
            //加法
            result.append(a).append(" + ").append(b).append(" =").append(a + b);
        } else if (type == SUB) {
            //减法
            result.append(a + b).append(" - ").append(b).append(" =").append(a);
        } else if (type == MUL) {
            //乘法
            result.append(a).append(" × ").append(b).append(" =").append(a * b);
        } else if (type == DIV) {
            //除法
            result.append(a * b).append(" ÷ ").append(b).append(" =").append(a);
        }
        return result.toString();
    }

}
